﻿using AutoMapper;
using MediatR;
using Provenance.Domain.DeepProcess;
using Provenance.ErrorCount;
using Provenance.Infra.Interfaces.DeepProcess;
using Provenance.Infra.Repositories.DeepProcess;
using Provenance.Read.Api.Application.Command.DeepProcess;

namespace Provenance.Read.Api.Application.CommandHandler.DeepProcess
{
    public class BackAdditiveCommandHandler : IRequestHandler<BackAdditiveCommand, ApiResult<Additive>>
    {
        private readonly IAdditiveRepository _additiveRepository;
        private readonly ILogger<BackAdditiveCommandHandler> _logger;
        private readonly IMapper _mapper;
        public BackAdditiveCommandHandler(IAdditiveRepository additiveRepository, ILogger<BackAdditiveCommandHandler> logger, IMapper mapper)
        {
            _additiveRepository = additiveRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult<Additive>> Handle(BackAdditiveCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var obj = await _additiveRepository.GetAsync(x => x.ProductInfoId == request.ProductInfoId);
                return ApiResult<Additive>.Success(obj);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return ApiResult<Additive>.Failure(ex.Message);
            }
        }
    }
}
